package cn.exrick.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.exrick.common.exception.XmallException;
import cn.exrick.mapper.TbPanelContentMapper;
import cn.exrick.pojo.TbItem;
import cn.exrick.pojo.TbItemSkuGoodCat;
import cn.exrick.pojo.TbItemSkuGoodCatAttr;
import cn.exrick.pojo.TbPanelContent;
import cn.exrick.pojo.TbPanelContentExample;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.pojo.common.Result;
import cn.exrick.pojo.dto.EsInfo;
import cn.exrick.pojo.dto.ItemDto;
import cn.exrick.service.ItemService;
import cn.exrick.service.ItemSkuCatAttrService;
import cn.exrick.service.ItemSkuCatService;
import cn.exrick.service.ItemSkuGoodCatAttrService;
import cn.exrick.service.ItemSkuGoodCatService;
import cn.exrick.service.SearchItemService;
import cn.exrick.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Created by Exrick on 2017/7/29.
 */
@RestController
@Api(description = "商品列表信息")
public class ItemController {

	private final static Logger log = LoggerFactory.getLogger(ItemController.class);

	@Autowired
	private ItemService itemService;

	@Autowired
	private ItemSkuCatService itemSkuCatService;
	@Autowired
	private ItemSkuCatAttrService itemSkuCatAttrService;

	@Autowired
	private ItemSkuGoodCatService itemSkuGoodCatService;

	@Autowired
	private ItemSkuGoodCatAttrService itemSkuGoodCatAttrService;

	@Autowired
	private SearchItemService searchItemService;

	@Autowired
	private TbPanelContentMapper tbPanelContentMapper;

	@RequestMapping(value = "/item/{itemId}", method = RequestMethod.GET)
	@ApiOperation(value = "通过ID获取商品")
	public Result<ItemDto> getItemById(@PathVariable Long itemId) {

		ItemDto itemDto = itemService.getItemById(itemId);
		TbItemSkuGoodCatAttr itemSkuGoodCatAttr = null;
		try {
			itemSkuGoodCatAttr = itemSkuGoodCatAttrService.findByItemId(itemDto.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<TbItemSkuGoodCat> itemSkuGoodCatList = itemSkuGoodCatAttrService
				.findOneTreeByAttrId(itemSkuGoodCatAttr.getId());
		String sku = "[";
		if (!CollectionUtils.isEmpty(itemSkuGoodCatList)) {
			for (TbItemSkuGoodCat itemSkuGoodCat : itemSkuGoodCatList) {
				sku += itemSkuGoodCat.getName() + ":" + itemSkuGoodCat.getAttr().getName() + "-";
			}
		}
		if (sku.endsWith("-")) {
			sku = sku.substring(0, sku.length() - 1);
		}
		sku += "]";
		itemDto.setSkuTitle(sku);
		return new ResultUtil<ItemDto>().setData(itemDto);
	}

	@RequestMapping(value = "/item/list", method = RequestMethod.GET)
	@ApiOperation(value = "分页搜索排序获取商品列表")
	public DataTablesResult getItemList(int draw, int start, int length, int cid,
			@RequestParam("search[value]") String search, @RequestParam("order[0][column]") int orderCol,
			@RequestParam("order[0][dir]") String orderDir, String searchItem, String minDate, String maxDate) {

		// 获取客户端需要排序的列
		String[] cols = { "checkbox", "id", "image", "title", "sell_point", "price", "created", "updated", "status" };
		String orderColumn = cols[orderCol];
		if (orderColumn == null) {
			orderColumn = "created";
		}
		// 获取排序方式 默认为desc(asc)
		if (orderDir == null) {
			orderDir = "desc";
		}
		DataTablesResult result = itemService.getItemList(draw, start, length, cid, search, orderColumn, orderDir);
		for (TbItem item : (List<TbItem>) result.getData()) {
			TbItemSkuGoodCatAttr itemSkuGoodCatAttr = itemSkuGoodCatAttrService.findByItemId(item.getId());
			List<TbItemSkuGoodCat> itemSkuGoodCatList = itemSkuGoodCatAttrService
					.findOneTreeByAttrId(itemSkuGoodCatAttr.getId());
			String sku = "[";
			if (!CollectionUtils.isEmpty(itemSkuGoodCatList)) {
				for (TbItemSkuGoodCat itemSkuGoodCat : itemSkuGoodCatList) {
					sku += itemSkuGoodCat.getName() + ":" + itemSkuGoodCat.getAttr().getName() + "-";
				}
			}
			if (sku.endsWith("-")) {
				sku = sku.substring(0, sku.length() - 1);
			}
			sku += "]";
			item.setSkuTitle(sku);
		}
		return result;
	}

	@RequestMapping(value = "/item/listSearch", method = RequestMethod.GET)
	@ApiOperation(value = "多条件分页搜索排序`获取商品列表")
	public DataTablesResult getItemSearchList(int draw, int start, int length, int cid, String searchKey,
			String minDate, String maxDate, @RequestParam("search[value]") String search,
			@RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir) {

		// 获取客户端需要排序的列
		String[] cols = { "checkbox", "id", "image", "title", "sell_point", "price", "created", "updated", "status" };
		// 默认排序列
		String orderColumn = cols[orderCol];
		if (orderColumn == null) {
			orderColumn = "created";
		}
		// 获取排序方式 默认为desc(asc)
		if (orderDir == null) {
			orderDir = "desc";
		}
		if (!search.isEmpty()) {
			searchKey = search;
		}
		DataTablesResult result = itemService.getItemSearchList(draw, start, length, cid, searchKey, minDate, maxDate,
				orderColumn, orderDir);
		for (TbItem item : (List<TbItem>) result.getData()) {
			TbItemSkuGoodCatAttr itemSkuGoodCatAttr = itemSkuGoodCatAttrService.findByItemId(item.getId());
			List<TbItemSkuGoodCat> itemSkuGoodCatList = itemSkuGoodCatAttrService
					.findOneTreeByAttrId(itemSkuGoodCatAttr.getId());
			String sku = "[";
			if (!CollectionUtils.isEmpty(itemSkuGoodCatList)) {
				for (TbItemSkuGoodCat itemSkuGoodCat : itemSkuGoodCatList) {
					sku += itemSkuGoodCat.getName() + ":" + itemSkuGoodCat.getAttr().getName() + "-";
				}
			}
			if (sku.endsWith("-")) {
				sku = sku.substring(0, sku.length() - 1);
			}
			sku += "]";
			item.setSkuTitle(sku);
		}
		return result;
	}

	@RequestMapping(value = "/item/count", method = RequestMethod.GET)
	@ApiOperation(value = "获得商品总数目")
	public DataTablesResult getAllItemCount() {

		DataTablesResult result = itemService.getAllItemCount();
		return result;
	}

	@RequestMapping(value = "/item/stop/{id}", method = RequestMethod.PUT)
	@ApiOperation(value = "下架商品")
	public Result<TbItem> stopItem(@PathVariable Long id) {

		TbItem tbItem = itemService.alertItemState(id, 0);
		return new ResultUtil<TbItem>().setData(tbItem);
	}

	@RequestMapping(value = "/item/start/{id}", method = RequestMethod.PUT)
	@ApiOperation(value = "发布商品")
	public Result<TbItem> startItem(@PathVariable Long id) {

		TbItem tbItem = itemService.alertItemState(id, 1);
		return new ResultUtil<TbItem>().setData(tbItem);
	}

	@RequestMapping(value = "/item/del/{ids}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除商品")
	public Result<Object> deleteItem(@PathVariable Long[] ids) {

		// 判断首页是否关联
		for (Long id : ids) {
			TbPanelContentExample example = new TbPanelContentExample();
			TbPanelContentExample.Criteria criteria = example.createCriteria();
			criteria.andProductIdEqualTo(id);
			List<TbPanelContent> list = tbPanelContentMapper.selectByExample(example);
			if (list != null && list.size() > 0) {
				return new ResultUtil<Object>().setErrorMsg("删除失败！包含首页展示关联的商品，请先从首页配置中删除");
			}
		}

		for (Long id : ids) {
			itemService.deleteItem(id);
		}
		return new ResultUtil<Object>().setData(null);
	}

	@RequestMapping(value = "/item/add", method = RequestMethod.POST)
	@ApiOperation(value = "添加商品")
	public Result<String> addItem(ItemDto itemDto, String sku) throws Exception {
		if (StringUtils.isEmpty(sku) || "{}".equals(sku)) {
			throw new Exception("请勾选sku");
		}
		itemDto.setSales(0L);
		itemService.addItems(sku, itemDto);
		return new ResultUtil<String>().setData("success");
	}

	@RequestMapping(value = "/item/update/{id}", method = RequestMethod.POST)
	@ApiOperation(value = "编辑商品")
	public Result<TbItem> updateItem(@PathVariable Long id, ItemDto itemDto) {

		TbItem tbItem = itemService.updateItem(id, itemDto);
		return new ResultUtil<TbItem>().setData(tbItem);
	}

	@RequestMapping(value = "/es/getInfo", method = RequestMethod.GET)
	@ApiOperation(value = "获取ES信息")
	public Result<Object> getESInfo() {

		EsInfo esInfo = searchItemService.getEsInfo();
		return new ResultUtil<Object>().setData(esInfo);
	}

	@RequestMapping(value = "/item/importIndex", method = RequestMethod.GET)
	@ApiOperation(value = "导入商品索引至ES")
	public Result<Object> importIndex() {

		searchItemService.importAllItems();
		return new ResultUtil<Object>().setData(null);
	}

	@RequestMapping(value = "/item/update/sales/{id}", method = RequestMethod.POST)
	@ApiOperation(value = "编辑商品")
	public Result<TbItem> updateItemSales(@PathVariable Long id, Long sales) {

		ItemDto itemDto = itemService.getItemById(id);
		if (itemDto == null) {
			throw new XmallException("商品不存在");
		}
		if (sales == null) {
			throw new XmallException("参数不足");
		}
		if (sales < 0) {
			throw new XmallException("参数错误");
		}
		itemDto.setSales(sales);
		TbItem item = itemService.updateItem(itemDto.getId(), itemDto);
		return new ResultUtil<TbItem>().setData(item);
	}
}
